package com.scoy.honeymei.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.zxing.common.StringUtils;
import com.lzy.okgo.model.HttpParams;
import com.qq.e.comm.util.StringUtil;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.mall.BookActivity;
import com.scoy.honeymei.activity.mall.BookDetailActivity;
import com.scoy.honeymei.adapter.BookListAdapter;
import com.scoy.honeymei.adapter.WithdrawAdapter;
import com.scoy.honeymei.base.BaseFragment;
import com.scoy.honeymei.bean.BaseBean;
import com.scoy.honeymei.bean.FilmOrderBean;
import com.scoy.honeymei.bean.WithdrawBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 电影票
 */
public class DypFragment extends BaseFragment {

    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private Context mContext;
    private ArrayList<FilmOrderBean.OrderBean> datalist;
    private BookListAdapter bookAdapter;
    private int type;
    private Unbinder unbinder;
    private int pageInt;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    public static DypFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        DypFragment fragment = new DypFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.movie_fragment_list, null, false);
        unbinder = ButterKnife.bind(this, view);
        mContext = getContext();
        initNormal();
        return view;
    }

    @Override
    public void initNormal() {
        datalist = new ArrayList<>();
        initRV();

        norSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(1500);
                datalist.clear();
                httpFilmOrder(1);
            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(1500);
                if (pageInt == -1) {
                    MyUtil.mytoast(mContext, getString(R.string.nomore));
                } else {
                    httpFilmOrder(pageInt);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private void initRV() {
        bookAdapter = new BookListAdapter(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(bookAdapter);
        bookAdapter.setOnOneClick(new BookListAdapter.OnOneClick() {
            @Override
            public void oneClick(int pos) {
                FilmOrderBean.OrderBean orderBean = datalist.get(pos);
                Intent intent = new Intent(mContext, BookDetailActivity.class);
                intent.putExtra("fId", orderBean.getId());
                startActivity(intent);
            }
        });

        bookAdapter.setOnLongClickListener(new BookListAdapter.OnLongClickListener() {
            @Override
            public void onLongClick(int pos) {
                FilmOrderBean.OrderBean orderBean = datalist.get(pos);
                if(orderBean.getStatus() == 1 || orderBean.getStatus() == -2){
                    //删除
                    deleteOrderDialog(orderBean.getId()+"");
                }
            }
        });
    }

    public void deleteOrderDialog(String orderId) {
        Dialog dialogNotice = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_tishi, null);
        TextView notTitleTv = view.findViewById(R.id.not_title_tv);
        TextView cancelBtn = view.findViewById(R.id.cancelBtn);
        TextView content = view.findViewById(R.id.content);
        TextView sureBtn = view.findViewById(R.id.sureBtn);
        notTitleTv.setText("提示");
        content.setText("确认删除该订单？");
        sureBtn.setText("确定");

        dialogNotice.setContentView(view);
        dialogNotice.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogNotice.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) mContext.getResources().getDimension(R.dimen.dp_300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogNotice.dismiss();
            }
        });
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delOrder(orderId);
                dialogNotice.dismiss();
            }
        });
        dialogNotice.show();
    }


    private void delOrder(String orderId) {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("id", orderId);


        Log.i("请求参数：", SPHelper.getString(MeConstant.UID, "")+"   "+orderId+"    "+type);

        HpGo.newInstance().HttpGo(getActivity(), MyUrl.DELETE_ORDER, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                Log.i("结果：",(String) o);
                if(StringUtil.isEmpty(o)){
                    datalist.clear();
                    httpFilmOrder(1);
                }
//                BaseBean resObj = new Gson().fromJson((String) o, BaseBean.class);
//                if("200".equals(resObj.getCode())){
//                    datalist.clear();
//                    httpFilmOrder(1);
//                }
            }
        });
    }


    private void httpFilmOrder(int myPage) {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("page", myPage);
        params.put("status", type);


        Log.i("请求参数：", SPHelper.getString(MeConstant.UID, "")+"   "+myPage+"    "+type);

        HpGo.newInstance().HttpGo(getActivity(), MyUrl.FIIM_ORDER_LISTS, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                Log.i("结果：",(String) o);
                FilmOrderBean filmOrderBean = new Gson().fromJson((String) o, FilmOrderBean.class);
                List<FilmOrderBean.OrderBean> order = filmOrderBean.getOrder();
                int next = filmOrderBean.getNext();
                if (next != 0) {
                    pageInt = myPage + 1;
                } else {
                    pageInt = -1;
                }

                datalist.addAll(order);
                bookAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        datalist.clear();
        httpFilmOrder(1);
    }
}
